﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Spellchecker
{
    public class DictionaryExpander
    {
        public static Dictionary< string,bool> ExpandDict( Dictionary< string,bool> input )
        {
            var result = new Dictionary<string, bool>();
            foreach( var s in input.Keys )
            {
                var tokens = s.Split(',');
                if ( tokens.Length == 1 )
                {
                    result[s] = true;
                }
                else
                {
                    var words = ExpandOneGroupOfWords(tokens );
                    foreach( var word in words )
                    {
                        result[word] = true;
                    }
                }
            }
            return result;
        }

        private static List< string > ExpandOneGroupOfWords(string[] tokens )
        {
            List< string > result = new List<string>();
            string baseWord = "";
            foreach (var token in tokens)
            {
                if (token.Contains('~'))
                {
                    string combined = token.Replace("~", baseWord);
                    result.Add(combined );
                }
                else
                {
                    baseWord = token;
                    result.Add( baseWord );
                }
            }
            return result;
        }
    }
}
